#ifndef __SEMAPHORE_H__
#define __SEMAPHORE_H__


#include "../SyncMethod/IMethod.h"


namespace Sync
{

class Semaphore : public IMethod
{
	HANDLE _semaphore;
	long _init_count;
	long _max_count;


public :
	Semaphore( const char* sync_title, long init_count, long max_count ) : IMethod( sync_title )
	{
		_init_count = init_count;
		_max_count = max_count;
	}
	virtual ~Semaphore( void ) {}


	virtual void release( void ) override
	{
		CloseHandle( _semaphore );
	}

	virtual void initialize( void ) override
	{
		_semaphore = CreateSemaphore( 0, _init_count, _max_count, 0 );

		std::cout << name().c_str() << std::endl;
	}

	virtual bool lock( void ) override
	{
		WaitForSingleObject( _semaphore, INFINITE ) ; 

		return true;
	}

	virtual void unlock( void ) override
	{
		ReleaseSemaphore( _semaphore, 1, NULL );
	}
};

};

#endif